package com.utonw.carloan.activity.applyloan;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.utonw.carloan.R;
import com.utonw.carloan.activity.base.BaseActivity;
import com.utonw.carloan.bean.applyloan.LoanPlanBean;
import com.utonw.carloan.function.GlobalLoanFunction;
import com.utonw.carloan.global.Constants;
import com.utonw.carloan.global.ReleaseData;
import com.utonw.carloan.net.NetCallBack;
import com.utonw.carloan.net.NetTool;
import com.utonw.carloan.utils.ChooseBarUtils;
import com.utonw.carloan.utils.LogUtil;
import com.utonw.carloan.utils.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by SEELE on 2017/3/21.
 */

/**
 * 申请条件界面，此界面是申请贷款流程的入口
 */
public class LoanEntryActivity extends BaseActivity {
    //申请贷款按钮
    private TextView mBeginLoan;
    //查看还款计划
    private TextView mShowRepayPlan;
    //注册广播
    private CommitBroadCastReceiver mCommitReceiver;
    /**
     * 默认值
     */
    //分期期限
    private TextView loanLimitTime;
    //抵押方式
    private TextView pledgeType;
    //还款方式
    private TextView repayWay;
    //还款计划方案
    private TextView planType;
    //购车方式
    private TextView buyCarMethod;
    //借款用途
    private TextView loanPurpose;
    //借款金额
    private EditText applyMoney;
    //月利率
    private EditText loanRate;
    //GPS流量费
    private EditText drunkDrive;
    //综合服务费
    private EditText depreciationFee;
    //办理费
    private EditText gpsCost;
    //停车费
    private EditText parkCost;
    //停车费布局,调整时会隐藏
    private LinearLayout parkCostLayout;

    @Override
    public int setLayout() {
        return R.layout.activity_applyloan_entry;
    }

    @Override
    public void initView() {
        mBeginLoan = (TextView) findViewById(R.id.tv_btn_begin_loan);
        mShowRepayPlan = (TextView) findViewById(R.id.tv_btn_show_repay_plan);
        parkCostLayout = (LinearLayout) findViewById(R.id.tingchefei);
    }

    @Override
    public void initData() {
        //初始化广播接收器
        mCommitReceiver = new CommitBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.LOAN_COMMIT_RECEIVER);
        registerReceiver(mCommitReceiver, intentFilter);
        //初始化PickerView选择器
        final GlobalLoanFunction globalLoanFunction = new GlobalLoanFunction();
        //设置ChooseBar标题
        ChooseBarUtils titleUtils = new ChooseBarUtils(this) {
            @Override
            protected void setChooseBarClickListener(int view) {

            }

            @Override
            protected void setChooseBarClickListener(int view, TextView toShow) {

                switch (view) {

                    case R.id.fenqiqixian:

                        final ArrayList<String> data1 = new ArrayList<>();
                        for (int i = 1; i < 37; i++) {
                            data1.add(i + "期");
                        }
                        globalLoanFunction.setTempData(data1, toShow).oriPickerView(LoanEntryActivity.this);

                        break;

                    case R.id.diyafangshi:
                        final ArrayList<String> data2 = new ArrayList<>();
                        data2.add("押车");
                        data2.add("押证");

                        globalLoanFunction.setTempData(data2, toShow).oriPickerView(LoanEntryActivity.this, new GlobalLoanFunction.OnClickPickerViewInterface() {
                            @Override
                            public void onClickPickerView() {
                                setDefaultValue();
                            }
                        });

                        break;

                    case R.id.huankuanfangshi:
                        final ArrayList<String> data3 = new ArrayList<>();
                        data3.add("先息后本");
                        data3.add("等本等息");

                        globalLoanFunction.setTempData(data3, toShow).oriPickerView(LoanEntryActivity.this, new GlobalLoanFunction.OnClickPickerViewInterface() {
                            @Override
                            public void onClickPickerView() {
                                setDefaultValue();
                            }
                        });

                        break;

                    case R.id.huankuanjihuafangan:
                        final ArrayList<String> data4 = new ArrayList<>();
                        data4.add("综合服务费按月收取");
                        data4.add("综合服务费一次性收取");

                        globalLoanFunction.setTempData(data4, toShow).oriPickerView(LoanEntryActivity.this);

                        break;

                    case R.id.gouchefangshi:
                        final ArrayList<String> data5 = new ArrayList<>();
                        data5.add("全款");
                        data5.add("按揭");

                        globalLoanFunction.setTempData(data5, toShow).oriPickerView(LoanEntryActivity.this);

                        break;

                    case R.id.jiekuanyongtu:
                        final ArrayList<String> data6 = new ArrayList<>();
                        data6.add("生意周转");
                        data6.add("采购原材料");
                        data6.add("房屋装修");
                        data6.add("发工资");
                        data6.add("支付货款");
                        data6.add("其他");

                        globalLoanFunction.setTempData(data6, toShow).oriPickerView(LoanEntryActivity.this);

                        break;

                }


            }

            @Override
            protected void getCheckedViewListener(int includeId, int sTitle, View toCheck) {
                switch (includeId) {
                    case R.id.fenqiqixian:
                        loanLimitTime = (TextView) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.diyafangshi:
                        pledgeType = (TextView) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.huankuanfangshi:
                        repayWay = (TextView) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.huankuanjihuafangan:
                        planType = (TextView) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.gouchefangshi:
                        buyCarMethod = (TextView) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.jiekuanyongtu:
                        loanPurpose = (TextView) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.jiekuanjine:
                        applyMoney = (EditText) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.yuelilv:
                        loanRate = (EditText) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.gpsliuliangfei:
                        drunkDrive = (EditText) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.zonghefuwufei:
                        depreciationFee = (EditText) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.banlifei:
                        gpsCost = (EditText) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                    case R.id.tingchefei:
                        parkCost = (EditText) toCheck;
                        setCheckViews(sTitle, toCheck);
                        break;

                }

            }

            @Override
            public void onToggleButtonListener(int buttonView, boolean isChecked) {

            }

            @Override
            public void getEditTextWithTipsListener(int includeId, EditText tempEt1) {

            }

            @Override
            public void onRadioButtonListener(RadioButton rMale, RadioButton rFemale) {

            }

            @Override
            public void onImageChooseBarListener(int view, ImageView mIndicator, TextView mInstruction, int sTitle) {

            }

            @Override
            public void getChangeWordsEditTextListener(int includeId, EditText tempEt1, int sTitle) {

            }

            @Override
            public void setVerificationListener() {

            }

        };
        titleUtils.initChooseBar("4","1","applyMoney", 0, R.id.jiekuanjine, R.string.apply_page_begin_price, ChooseBarUtils.ResultState.EDIT_TEXT_WITH_TIPS, "（万元）");
        titleUtils.initChooseBar("0","1","loanLimitTime", 0, R.id.fenqiqixian, R.string.apply_page_begin_date);
        titleUtils.initChooseBar("5","1","pledgeType", 0, R.id.diyafangshi, R.string.apply_page_begin_pledge);
        titleUtils.initChooseBar("6","1","repayWay", 0, R.id.huankuanfangshi, R.string.apply_page_begin_repay);
        titleUtils.initChooseBar("7","1","loanRate", 0, R.id.yuelilv, R.string.apply_page_begin_month_rate, ChooseBarUtils.ResultState.EDIT_TEXT_WITH_TIPS, "%");
        titleUtils.initChooseBar("0","1","planType", 0, R.id.huankuanjihuafangan, R.string.apply_page_begin_repay_method);
        titleUtils.initChooseBar("0","1","buyCarMethod", 0, R.id.gouchefangshi, R.string.apply_page_begin_buycar);
        titleUtils.initChooseBar("0","1","loanPurpose", 0, R.id.jiekuanyongtu, R.string.apply_page_begin_loan_to_do);
        titleUtils.initChooseBar("0","1","drunkDrive", 0, R.id.gpsliuliangfei, R.string.apply_page_begin_gps, ChooseBarUtils.ResultState.EDIT_TEXT_WITH_TIPS, "元/月");
        titleUtils.initChooseBar("0","1","depreciationFee", 0, R.id.zonghefuwufei, R.string.apply_page_begin_service, ChooseBarUtils.ResultState.EDIT_TEXT_WITH_TIPS, "%/月");
        titleUtils.initChooseBar("0","1","gpsCost", 0, R.id.banlifei, R.string.apply_page_begin_pay_to, ChooseBarUtils.ResultState.EDIT_TEXT_WITH_TIPS, "元");
        titleUtils.initChooseBar("0","1","parkCost", 0, R.id.tingchefei, R.string.apply_page_begin_park_cost, ChooseBarUtils.ResultState.EDIT_TEXT_WITH_TIPS, "元");
        titleUtils.initChooseBar("0","0","", -1, R.id.shenqingzige, R.string.apply_page_begin_seniority);
        titleUtils.initChooseBar("0","0","", -1, R.id.suoxucailiao, R.string.apply_page_begin_needing);

        //初始化默认值
        initDefaultData();

        //申请借款 按钮
        mBeginLoan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到第二步
                if (checkEmpty()) {
                    startAnActivity(new Intent(LoanEntryActivity.this, LoanContentActivity.class));
                }

            }
        });


        //打开还款计划页面
        mShowRepayPlan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkEmpty()) {
                    if (Double.valueOf(applyMoney.getText().toString()) < 0.01) {
                        UIUtils.showShortToast("贷款金额必须大于0.01（万元）");
                    } else {

                        Map<String, String> loanPlanMap = new HashMap<>();
                        loanPlanMap.put("applyMoney", applyMoney.getText().toString());
                        loanPlanMap.put("loanLimitTime", subLimitTime(loanLimitTime.getText().toString()));
                        loanPlanMap.put("loanRate", loanRate.getText().toString());
                        loanPlanMap.put("repayWay", repayWay.getText().toString());
                        loanPlanMap.put("planType", planType.getText().toString());
                        loanPlanMap.put("gpsCost", gpsCost.getText().toString());
                        loanPlanMap.put("drunkDrive", drunkDrive.getText().toString());
                        loanPlanMap.put("pledgeType", pledgeType.getText().toString());
                        loanPlanMap.put("depreciationFee", depreciationFee.getText().toString());
                        loanPlanMap.put("parkCost", parkCost.getText().toString());

                        NetTool.getInstance().startRequest(LoanEntryActivity.this, true, Constants.LOANPLAN, loanPlanMap, LoanPlanBean.class
                                , new NetCallBack<LoanPlanBean>() {
                                    @Override
                                    public void onSuccess(LoanPlanBean response) {

                                        if (response.getRetCode().equals("000000")) {
                                            //跳转独立界面
                                            Intent intent = new Intent(LoanEntryActivity.this, ShowRepayPlanActivity.class);
                                            intent.putExtra("planList", response);
                                            startAnActivity(intent);


                                        } else {

                                            UIUtils.showShortToast(response.getMessage());
                                        }
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        LogUtil.d(e.getMessage());
                                    }
                                });
                    }
                } else {

                }

            }
        });

    }

    private String subLimitTime(String s) {
        return s.substring(0, s.length() - 1);
    }

    private void initDefaultData() {
        //抵押方式
        pledgeType.setText("押车");
        pledgeType.setTextColor(UIUtils.getColor(R.color.fontColor));
        //还款方式
        repayWay.setText("等本等息");
        repayWay.setTextColor(UIUtils.getColor(R.color.fontColor));
        //还款计划方案
        planType.setText("综合服务费按月收取");
        planType.setTextColor(UIUtils.getColor(R.color.fontColor));
        //购车方式
        buyCarMethod.setText("全款");
        buyCarMethod.setTextColor(UIUtils.getColor(R.color.fontColor));
        //借款用途
        loanPurpose.setText("生意周转");
        loanPurpose.setTextColor(UIUtils.getColor(R.color.fontColor));

        //加载默认值控制器
        setDefaultValue();

    }

    private void setDefaultValue() {
        if (pledgeType.getText().toString().equals("押证")) {

            if (repayWay.getText().toString().equals("等本等息")) {
                //月利率
                loanRate.setText("0.68");
                //分期期限
                loanLimitTime.setText("12期");
                loanLimitTime.setTextColor(UIUtils.getColor(R.color.fontColor));
                //gps流量费
                drunkDrive.setText("100");
                //综合服务费
                depreciationFee.setText("1");
                //办理费
                gpsCost.setText("800");
                //取消停车费价格
                parkCost.setText("0");
                //隐藏停车费
                parkCostLayout.setVisibility(View.GONE);
            } else if (repayWay.getText().toString().equals("先息后本")) {
                //月利率
                loanRate.setText("0.68");
                //分期期限
                loanLimitTime.setText("3期");
                loanLimitTime.setTextColor(UIUtils.getColor(R.color.fontColor));
                //gps流量费
                drunkDrive.setText("150");
                //综合服务费
                depreciationFee.setText("2");
                //办理费
                gpsCost.setText("800");
                //取消停车费价格
                parkCost.setText("0");
                //隐藏停车费
                parkCostLayout.setVisibility(View.GONE);

            }
        }

        if (pledgeType.getText().toString().equals("押车")) {

            if (repayWay.getText().toString().equals("等本等息")) {
                //月利率
                loanRate.setText("0.68");
                //分期期限
                loanLimitTime.setText("12期");
                loanLimitTime.setTextColor(UIUtils.getColor(R.color.fontColor));
                //gps流量费
                drunkDrive.setText("0");
                //综合服务费
                depreciationFee.setText("1");
                //办理费
                gpsCost.setText("600");
                //取消停车费价格
                parkCost.setText("0");
                //隐藏停车费
                parkCostLayout.setVisibility(View.GONE);
            } else if (repayWay.getText().toString().equals("先息后本")) {
                //月利率
                loanRate.setText("0.88");
                //分期期限
                loanLimitTime.setText("3期");
                loanLimitTime.setTextColor(UIUtils.getColor(R.color.fontColor));
                //gps流量费
                drunkDrive.setText("0");
                //综合服务费
                depreciationFee.setText("1.5");
                //办理费
                gpsCost.setText("600");
                //设置停车费价格
                parkCost.setText("600");
                //隐藏停车费
                parkCostLayout.setVisibility(View.VISIBLE);

            }
        }

    }


    @Override
    public String setTitle() {
        return getString(R.string.apply_condition);
    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(mCommitReceiver);
        ReleaseData.getAllTextViewEntry.clear();
        ReleaseData.getAllEditTextEntry.clear();
        for (int i = 0; i < ReleaseData.tempList.size(); i++) {
            ReleaseData.tempList.get(i).clear();
        }
        ReleaseData.tempList.clear();
        ReleaseData.getAllSwitchEntry.clear();
        ReleaseData.getAllRadioGroupEntry.clear();
        ReleaseData.getAllImageUpload.clear();
        ReleaseData.getAllResultEntry.clear();
        super.onDestroy();
    }

    //定义广播接收器
    class CommitBroadCastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }

}
